class Solution {
    public String multiply(String num1, String num2) {
        if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }
        String ans = "0";
        for (int i = num2.length() - 1; i >= 0; i--) {
            StringBuilder cur = new StringBuilder();
            // 加0
            for (int j = num2.length() - 1; j > i; j--) {
                cur.append("0");
            }
            // 进位
            int add = 0;
            int y = num2.charAt(i) - '0';
            for (int j = num1.length() - 1; j>= 0; j--) {
                int x = num1.charAt(j) - '0';
                int product = x * y + add;
                cur.append(product % 10);
                add = product / 10;
            }
            if (add != 0) {
                cur.append(add % 10);
            }
            ans = addString(ans, cur.reverse().toString());
        }
        return ans;
    }
    public String addString(String s1, String s2) {
        int i = s1.length() - 1, j = s2.length() - 1;
        int t = 0;
        StringBuilder ret = new StringBuilder();
        while (i >= 0 || j >= 0 || t != 0) {
            if (i >= 0) t += s1.charAt(i--) - '0';
            if (j >= 0) t += s2.charAt(j--) - '0';
            ret.append((t % 10));
            t /= 10;
        }
        return ret.reverse().toString();
    }
}